home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / NuBus⁄Slot Manager / ROM Build⁄Download 3.2.4 / Data folder / Data.c next >
Encoding:
C/C++ Source or Header  |  1995-05-25  |  3.4 KB  |  117 lines  |  [TEXT/MPS ]

  1. /**********************************************************************
  2.  *
  3.  * Copyright Apple Computer, Inc. 1986, 1992, 1995
  4.  * All Rights Reserved
  5.  *
  6.  *    Description:     This program reads code segment 1 from a specified
  7.  *                    resource file and and writes it as a data file
  8.  *                    to an output file.  Run this tool after patching
  9.  *                    the CRC value with the CRCPatch tool.
  10.  *
  11.  *    Calling syntax: Data rsrcFileName DataFileName
  12.  *
  13.  *    Written:         July 30, 1986.
  14.  *
  15.  *    Modified:       09/16/92.  
  16.  *                    • Changed this header to be more readable.
  17.  *                    • Fixed compiler warnings, by:
  18.  *                      - deleting unused variable definitions
  19.  *                      - adding #include <FCntl.h>.
  20.  *                    • Fixed bug that caused possible bus error in 
  21.  *                      32 bit mode when skipping over first 4 bytes of 
  22.  *                      the resource header
  23.  *
  24.  **********************************************************************/
  25.                         
  26.  
  27. #include <stdio.h>
  28. #include <FCntl.h>
  29. #include <types.h>
  30. #include <osutils.h>
  31. #include <files.h>
  32. #include <resources.h>
  33. #include <memory.h>
  34. #include <errno.h>
  35.  
  36.  
  37.  
  38. pascal void _CalcCRC (SizeCode,CodePtr,crc)
  39. long        SizeCode;
  40. Ptr            CodePtr;
  41. extern;
  42.  
  43. /******************************************************************
  44.     Main
  45. *******************************************************************/
  46. main(argc,argv)
  47.  
  48. int      argc;
  49. char  *argv[];
  50.  
  51. {
  52.   short        refnum;
  53.   Handle    CodeHandle;        /* Handle to code resource */
  54.   short        IOR;            /* IO Result */
  55.   long        SizeCode;        /* Size of the code */
  56.   Ptr        CodePtr;        /* Pointer to the code */
  57.   int        FileDescript;    /* The file descriptor of the data file */
  58.   unsigned    NumBWritten;    /* Number of bytes written per write */
  59.   unsigned    Total;            /* Total number of bytes written */
  60.         
  61.   if (argc == 1)
  62.     {
  63.       fprintf(stderr,"### ERROR : No files specified.\n");
  64.       fprintf(stderr,"### SYNTAX: Data  rsrcFileName  DataFileName\n");
  65.       fprintf(stderr,"### DSCRPT: Copy code segment 1 to a data file.\n");
  66.     }
  67.   else if (argc != 3)
  68.     {
  69.       fprintf(stderr,"### ERROR : Wrong number of parameters specified.\n");
  70.       fprintf(stderr,"### SYNTAX: Data  rsrcFileName  DataFileName\n");
  71.       fprintf(stderr,"### DSCRPT: Copy code segment 1 to a data file.\n");
  72.     }
  73.   else
  74.     {
  75.       refnum = openresfile(argv[1]);
  76.       if (refnum < 0 )
  77.         fprintf(stderr,"### ERROR : Resource file: %s can't be opened. err = %d.\n",argv[1],refnum);
  78.       else
  79.         {
  80.            CodeHandle = GetResource('CODE',1);
  81.            HLock(CodeHandle);
  82.            IOR = ResError();
  83.            if (IOR != 0)
  84.              fprintf(stderr,"### ERROR : Code resource not available. Err = %d\n",errno);
  85.            else
  86.              {
  87.                SizeCode = GetHandleSize(CodeHandle);
  88.                SizeCode = SizeCode - 4;                                        /* Skip first 4 bytes (Resource header) */
  89.                CodePtr = ((Ptr)*CodeHandle) + 4;                            /* Skip first 4 bytes (Resource header) */
  90.         
  91.                FileDescript = creat(argv[2]);
  92.                if (FileDescript < 0)
  93.                  fprintf(stderr,"### ERROR : Data file: %s can't be opened. err = %d.\n",argv[2],errno);
  94.                else
  95.                  {
  96.                    Total = 0;
  97.                    while (Total < SizeCode)
  98.                      {
  99.                        NumBWritten = write(FileDescript,CodePtr,SizeCode);
  100.                        if (NumBWritten < 0)
  101.                          {
  102.                            fprintf(stderr,"### ERROR : Write err = %d.\n",errno);
  103.                            Total = SizeCode;
  104.                          }
  105.                        else
  106.                          Total = Total + NumBWritten;
  107.                      }
  108.                    close(FileDescript);
  109.                  }
  110.  
  111.                CloseResFile(refnum);
  112.              }
  113.         }
  114.     }
  115. }
  116.  
  117.